package com.huishen.edrivenew.net;

import org.apache.http.HttpEntity;
import org.apache.http.client.params.ClientPNames;

import android.content.Context;
import android.util.Log;

import com.huishen.edrivenew.Constants;
import com.huishen.edrivenew.util.TLog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ApiHttpClient {


    public static AsyncHttpClient client;


    public ApiHttpClient() {}

    public static AsyncHttpClient getHttpClient() {
        return client;
    }

    public static void cancelAll(Context context) {
        client.cancelRequests(context, true);
    }

    public static void cancelRequest(Context context) {
        client.cancelRequests(context, true);
    }


    public static void log(String log) {
        TLog.log("BaseApi", log);
    }


    public static String getAbsoluteApiUrl(String partUrl) {
//        String url = String.format(Constants.API_URL, partUrl);
        String url = Constants.API_URL + partUrl;
        Log.d("BASE_CLIENT", "request:" + url);
        return url;
    }
    
    public static void get(Context context, String partUrl, RequestParams params, AsyncHttpResponseHandler handler) {
        client.get(context, getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("GET ").append(partUrl).append("&")
            .append(params).toString());
    }

    public static void post(Context context, String partUrl, RequestParams params, AsyncHttpResponseHandler handler) {
        client.post(context, getAbsoluteApiUrl(partUrl), params, handler);
        
        log(new StringBuilder("POST ").append(partUrl).append("&")
            .append(params).toString());
    }

    public static void post(String partUrl, AsyncHttpResponseHandler handler) {
        client.post(getAbsoluteApiUrl(partUrl), handler);
        log(new StringBuilder("POST ").append(partUrl).toString());
    }

    public static void post(String partUrl, RequestParams params,
            AsyncHttpResponseHandler handler) {
        client.post(getAbsoluteApiUrl(partUrl), params, handler);
        log(new StringBuilder("POST ").append(partUrl).append("&")
                .append(params).toString());
    }

    public static void postDirect(Context context, String url, RequestParams params,
            AsyncHttpResponseHandler handler) {
        client.post(context, url, params, handler);
        log(new StringBuilder("POST ").append(url).append("&").append(params)
                .toString());
    }



    /**
     * post file
     */
    public static void post(Context context, String url, HttpEntity entity,
                            AsyncHttpResponseHandler handler) {
        client.post(context, url, null, entity, "", handler);

        log(new StringBuilder("POST FILE ").append(url)
                .toString());
    }



    public static void setHttpClient(AsyncHttpClient c) {
        client = c;
//        client.addHeader("Connection", "Keep-Alive");
        client.getHttpClient().getParams()
                .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    }


}
